package com.wzq.controller.pc;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.dao.pojo.Code;
import com.wzq.dao.pojo.Other;
import com.wzq.dao.pojo.Result;
import com.wzq.service.IOtherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/PC/other")
public class PCOtherController {

    @Autowired
    private IOtherService IOtherService;



    //    添加资源
    @PostMapping
    public Result save(@RequestBody Other other){
        boolean flag = IOtherService.save(other);
        return new Result(flag? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    //    删除资源
    @DeleteMapping
    public Result remove(@RequestBody List<Integer> ids) {
        boolean flag = IOtherService.removeByIds(ids);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }
    //    id查询资源其他信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable int id){
        LambdaQueryWrapper<Other> lqw = new LambdaQueryWrapper();
        lqw.eq(Other::getP_id,id).notIn(Other::getTitle,"使用须知");
        List<Other> list = IOtherService.list(lqw);
        int code = list !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

    //    id查询资源其他信息
    @GetMapping("/msag/{id}")
    public Result getMessage(@PathVariable int id){
        LambdaQueryWrapper<Other> lqw = new LambdaQueryWrapper();
        lqw.eq(Other::getP_id,id).eq(Other::getTitle,"使用须知");
        List<Other> list = IOtherService.list(lqw);
        int code = list !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

    //    修改资源
    @PutMapping
    public Result updateById(@RequestBody Other other) {
        boolean flag = IOtherService.updateById(other);
        return new Result(flag? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //    添加其他信息集合
    @PostMapping("/add")
    public Result save(@RequestBody List<Other>  others){
        try {
            for ( Other o: others){
                IOtherService.save(o);
            }
        } catch ( Exception e){
            return new Result(Code.SAVE_ERR,null);
        }
        return new Result(Code.SAVE_OK,null,"true");
    }
}

